package userlimitModel

import (
	"arabApi/app/api/models"
	"arabApi/app/api/requests"
	"arabApi/app/api/requests/userlimitRequest"
	"arabApi/pkg/database"
	"arabApi/pkg/helpers"
	"github.com/spf13/cast"
)

type Role struct {
	models.BaseModel
	RoleName string `gorm:"column:role_name" db:"role_name" json:"role_name"` //  项目名称
	Desc     string `gorm:"column:desc" db:"desc" json:"desc"`                //  描述
	models.CommonTimestampsField
}

func (Role) TableName() string {
	return "tp_role"
}

func RoleList(r requests.PageStruct) ([]Role, int64) {
	var count int64
	dd := database.DB.Model(Role{})

	var users []Role
	p := cast.ToInt(r.Page)
	ps := cast.ToInt(r.PageSize)

	dd.Count(&count).Limit(ps).Offset((p - 1) * ps).Find(&users)
	return users, count
}

func (Role) RoleAdd(r userlimitRequest.RoleAdd) uint64 {
	u := &Role{}
	if err := helpers.Copy(r, u); err != nil {
		return 0
	}
	database.DB.Create(u)
	return u.ID

}

func GetMsgByIdR(id int64) Role {
	var user Role
	database.DB.Model(Role{}).Where("id = ?", id).Find(&user)
	return user
}

func (Role) RoleEdit(r userlimitRequest.RoleAdd) error {
	u := &Role{}
	if err := helpers.Copy(r, u); err != nil {
		return err
	}
	res := database.DB.Where("id = ?", u.ID).Updates(u)
	return res.Error

}
